<template>
  <div class="loading" v-if="listLoading" v-loading="listLoading" element-loading-text="视频列表加载中~~"></div>
  <div class="videoList" v-else>
<!--    视频元素-->
    <div
      class="videoItem"
      v-for="video in videoList"
      :key="video.id"
    >
<!--      图片位置-->
      <div class="itemImg">
        <img :src="video.cover[0]" alt="">
        <div class="imgCover"></div>
      </div>
<!--      视频信息-->
      <div class="videoInfo">
        <div class="infoTop">
<!--          标题-->
          <div class="title">
            <span>{{ video.title }}</span>
          </div>
<!--          价格-->
          <div class="price">
            <div class="priceLeft">
              <span>￥</span>
              <span>{{Number(video.commodityBackup.price).toFixed(2)}}</span>
            </div>
            <div class="priceRight">
              <span>
                {{video.statisticsBackup.viewCount>10000?(video.statisticsBackup.viewCount/10000).toFixed(2) + 'W':video.statisticsBackup.viewCount}}人浏览
              </span>
            </div>
            <div class="free"></div>
          </div>
<!--          点赞量-->
          <div class="review">
            <span class="praiseCount">
              <el-icon><pointer/></el-icon>
              <span class="numInfo">{{video.statisticsBackup.praiseCount}}</span>
            </span>
            <span class="commentCount">
              <el-icon><comment/></el-icon>
              <span class="comInfo">{{ video.statisticsBackup.commentCount }}</span>
            </span>
            <span class="favoriteCount">
              <el-icon><star-filled/></el-icon>
              <span class="favInfo">{{ video.statisticsBackup.favoriteCount }}</span>
            </span>
          </div>
        </div>
        <div class="creatorInfo">
          <div class="creLeft">
            <div class="creator">
              <img :src="video.creatorBackup.avatar" alt="">
            </div>
            <span>{{video.creatorBackup.name}}</span>
            <img
              :src="video.creatorBackup.type==='supplier_consumer'?
              '/src/assets/Course_images/blueLogo.png':'/src/assets/Course_images/redLogo.png'"
              class="personal"
            >
          </div>
          <div class="creRight">
            <span>16-11-11</span>
          </div>
        </div>
      </div>
    </div>
    <div class="logMore" v-show="!moreLoading">
      <el-button type="warning" class="btnMore" @click="getMoreVideo">加载更多</el-button>
    </div>
    <div
      class="moreLoading"
      v-loading="moreLoading"
      v-show="moreLoading"
      element-loading-text="loading..."
      element-loading-background="transparent"
    ></div>
  </div>

</template>

<script lang="ts">
import {defineComponent} from 'vue';

export default defineComponent({
  name: 'VideoList',
});
</script>
<script setup lang="ts">
import {Pointer,Comment,StarFilled} from '@element-plus/icons-vue'
import {ref, onMounted} from 'vue';
//引入视频列表请求
import courseApi from '@/api/course/courseApi';
import {ElMessage} from 'element-plus';
onMounted(()=>{
  listLoading.value = true
  // console.log('videoProp',videoProp);
  getVideoList()

})
//创建排序的date数据
interface propsType {
  path:string,
  sort:string
}
const pageNum = ref(0)
const pageSize = ref(20)
const videoList = ref<any>([])
const videoProp = defineProps<propsType>()
//创建一个值关联loading加载动画
const listLoading = ref(false)
const getVideoList =async () =>{
  moreLoading.value = true
  try {
    const re = await courseApi
      .reqVideList(
        {sortType:videoProp.sort,path:videoProp.path},
        {pageNum:pageNum.value,pageSize:pageSize.value}
      );
    // videoList.value = [...videoList.value,...re.content]
    videoList.value = videoList.value.concat(re.content)
    listLoading.value = false
    moreLoading.value = false
  }catch (e) {
    ElMessage.warning(e as any)
  }
  // console.log('videoList',re);
}
const isUnder = ref(false)
//控制加载动画
const moreLoading = ref(false)
//监听停止滚动更新数据
const  scrollY = async () => {
  if (pageNum.value > 1) return
  let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  //  可视区域
  let clientHeight = document.documentElement.clientHeight;
  // 页面的高度
  let scrollHeight = document.documentElement.scrollHeight;
  if (scrollTop + clientHeight <= scrollHeight - 257)isUnder.value = false;
  //如果触底就让index++
  if (isUnder.value)return
  if (scrollTop + clientHeight >= scrollHeight - 257) {
    isUnder.value = true
    pageNum.value += 1
    try {
      await getVideoList()
    }catch (e){
      ElMessage.warning(e as any)
    }
    console.log('触底了');
  }
}
window.addEventListener('scroll',scrollY)
const getMoreVideo = () =>{
  pageNum.value += 1
  getVideoList()
}
</script>

<style lang="scss" scoped>
.loading{
  width: 1200px;
  height: 80px;
}
.videoList{
  width: 1260px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-top: 20px;
  .videoItem{
    width: 225px;
    height: 280px;
    background-color: #fff;
    margin-bottom: 40px;
    .itemImg{
      position: relative;
      width: 225px;
      height: 125px;
      &:hover{
        .imgCover{
          display: block;
        }
      }
      img{
        width: 225px;
        height: 125px;
      }
      .imgCover{
        position: absolute;
        top: 0;
        width: 225px;
        height: 125px;
        display: none;
        background-color: rgba(0,0,0,.3);
      }
    }
    .videoInfo{
      width: 225px;
      height: 155px;
      .infoTop{
        width: 100%;
        height: 104px;
        padding: 0 10px 13px;
        box-sizing:border-box;
        border-bottom: 1px solid #ebeced;
        .title{
          padding-top: 10px;
          height: 35px;
          width: 100%;
          span{
            cursor: pointer;
            line-height: 18px;
            font-size: 15px;
            overflow: hidden;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            text-overflow: ellipsis;
          }
        }
        .price{
          position: relative;
          width: 204px;
          height: 25px;
          padding-bottom: 7px;
          display: flex;
          justify-content: space-between;
          .priceLeft{
            font-size: 16px;
            line-height: 22px;
            color: #f93684;
          }
          .priceRight{
            color: #7d8090;
            font-size: 12px;
            line-height: 24px;
          }
          .free{
            position: absolute;
            top: 19px;
            left: 3px;
            width: 38px;
            height: 14px;
            background:url("/src/assets/Course_images/vip_free@3x.png") no-repeat center center;
            background-size: cover;
          }
        }
        .review{
          margin-top: 5px;
          width: 204px;
          height: 22px;
          padding-bottom: 6px;
          display: flex;
          color: #b1b5c1;
          .numInfo{
            margin-right: 18px;
          }
          .comInfo{
            margin-right: 18px;
          }
        }
      }
      .creatorInfo{
        width: 208px;
        height: 50px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-left: 16px;
        .creLeft{
          width: 96px;
          height: 24px;
          position: relative;
          .creator{
            width: 24px;
            height: 24px;
            top: 3px;
            left: -3px;
            position: absolute;
            img{
              width: 100%;
              height: 100%;
              border-radius: 50%;
            }
          }
          span{
            padding-left: 24px;
            font-size: 14px;
            line-height: 29px;
            overflow: hidden;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 1;
            text-overflow: ellipsis;
          }
          .personal{
            width: 12px;
            height: 12px;
            position: absolute;
            top: 8px;
            right: -16px;
          }
        }
        .creRight{
          color: #b1b5c1;
          padding-right: 8px;
        }
      }
    }
  }
  .logMore{
    width: 100%;
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    padding-bottom: 30px;
    .btnMore{
      width: 150px;
      height: 40px;
      font-size: 16px;
      line-height: 40px;
    }
  }
  .moreLoading{
    width: 100%;
    height: 80px;
  }
}
</style>
